const app = getApp()
var xrSdk = require('../../../vendor/hope-sdk/index.js');
const util = require('../../../utils/util.js');
var xrRequest = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showSuccess : false,

    showActionSheet: false,
    maskClosable: true,
    tips: "确认清空搜索历史吗？",
    itemList: [],
    color: "#9a9a9a",
    size: 26,
    isCancel: true,
    car:null,
    shop:null,
    position:'',
    tire_selection:[],

    l_f_tire: [],
    r_f_tire: [],
    l_e_tire: [],
    r_e_tire: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.init();
    that.clearTyre();
  },
  init:function(){
    var that = this;
    let car = xrSdk.cookie.get('car_info');
    if(car == null){
      util.toast('请选择车辆', 3000, 'none',()=>{
        setTimeout(()=>{wx.navigateBack({delta: 1})
        },2000);
      });
    }
    let shop = xrSdk.cookie.get('shop_info');
    if (shop == null) {
      util.toast('请选择安装门店', 3000, 'none', () => {
        setTimeout(() => {
          wx.navigateBack({ delta: 1 })
        }, 2000);
      });
    }
    let tire_selection = xrSdk.cookie.get('tire_selection');
    that.setData({
      car : car,
      shop: shop,
      tire_selection: tire_selection
    });
  },
  chooseClick:function(e){
    console.log(e);
    let itemList = [{
      text: "扫描条形码",
      color: "#1a1a1a"
    }, {
        text: "手动输入条形码",
      color: "#1a1a1a"
    }, {
        text: "手动输入规格",
      color: "#1a1a1a"
    }];
    let maskClosable = true;
    let tips = "请选择录入方式";
    let color = "#9a9a9a";
    let size = 32;
    let isCancel = true;
    setTimeout(() => {
      this.setData({
        showActionSheet: true,
        itemList: itemList,
        maskClosable: maskClosable,
        tips: tips,
        color: color,
        size: size,
        isCancel: isCancel,
        position:e.currentTarget.dataset.tyre
      })
    }, 0)
  },
  closeActionSheet: function () {
    this.setData({
      showActionSheet: false
    })
  },
  itemClick: function (e) {
    var that = this;
    let index = e.detail.index;
    //util.toast(`您点击的按钮索引为：${index}`)
    switch(index){
      case 0:  //扫码
        that.scanfHandle();
      break;
      case 1:  // 手动输入条码
        app.WxService.navigateTo("../inputbarcode/inputbarcode",{
          position: that.data.position
        });
      break;
      case 2:  //手动输入规格
        app.WxService.navigateTo("../inputdesc/inputdesc", {
          position: that.data.position
        });
      break
    }
    this.closeActionSheet();
  },
  scanfHandle:function(){
    var that = this;
    wx.scanCode({
      // onlyFromCamera:"true",//是否只能从相机扫码，不允许从相册选择图片
      scanType: 'barCode',
      success: function (res) {
        console.log(res);
        var barcode = res.result;
        if (barcode == '') {
          util.toast('轮胎讯息无法确认，请重新扫码');
          return;
        }		
        if (that.isRepeat(barcode)) {
          util.toast('该轮胎条码已存在');
          return;
        }
        that.findProduct(barcode);
      },
      fail: function (res) {
        util.toast('轮胎讯息无法确认，请重新扫码');
        console.log(res)
      }
    })

    //var barcode = '151102000347';
    // if (that.isRepeat(barcode)) {
    //   util.toast('该轮胎条码已存在');
    //   return;
    // }
    //that.findProduct(barcode);
  },
  findProduct: function (barcode) {
    var that = this;
    xrRequest.request(app,{
      url: config.service.findProduct,
      login: false,
      auth: false,
      hideLoading:true,
      data: {
        barcode: barcode
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {
          app.WxService.navigateTo("../confirm/confirm",{
            position : that.data.position,
            id       : res.data.data.id,
            barcode  : res.data.data.barcode,
            figure_desc: res.data.data.figure_desc,
            tyre_desc  : res.data.data.tyre_desc
          });
          wx.hideLoading();
        } else {
          util.toast(res.data.msg);
        }
      },
      fail: function (res) { 
        util.toast(res);
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  clearTyre:function(){
    var that = this;
    that.setData({
      l_f_tire: [],
      r_f_tire: [],
      l_e_tire: [],
      r_e_tire: []
    });
  },
  // 从缓存中读取轮胎信息
  initTyre:function(){
    var that = this;
    var l_f_tire = xrSdk.cookie.get('l_f_tire');
    var r_f_tire = xrSdk.cookie.get('r_f_tire'); 
    var l_e_tire = xrSdk.cookie.get('l_e_tire'); 
    var r_e_tire = xrSdk.cookie.get('r_e_tire'); 
    that.setData({
      l_f_tire: l_f_tire,
      r_f_tire: r_f_tire,
      l_e_tire: l_e_tire,
      r_e_tire: r_e_tire
    });

    let tire_selection = xrSdk.cookie.get('tire_selection');
    that.setData({
      tire_selection: tire_selection
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  isRepeat:function(barcode){
    var l_f_tire = xrSdk.cookie.get('l_f_tire');
    if (l_f_tire && l_f_tire.barcode != -1 && l_f_tire.barcode == barcode) {
        return true;
    }
    var r_f_tire = xrSdk.cookie.get('r_f_tire');
    if (r_f_tire && r_f_tire.barcode != -1 && r_f_tire.barcode == barcode) {
      return true;
    }
    var l_e_tire = xrSdk.cookie.get('l_e_tire');
    if (l_e_tire && l_e_tire.barcode != -1 && l_e_tire.barcode == barcode) {
      return true;
    }
    var r_e_tire = xrSdk.cookie.get('r_e_tire');
    if (r_e_tire && r_e_tire.barcode != -1 && r_e_tire.barcode == barcode) {
      return true;
    }
    return false;
  },
  /**
   * 检查轮胎信息填写完了没有
   */
  checkTiresCode:function(){
    var tire_selection = xrSdk.cookie.get('tire_selection');
    var isHave = false;
    if (tire_selection.l_f_selected) {
      isHave = true;
      if (!xrSdk.cookie.get('l_f_tire')) {
        return false;
      }
    }
    if (tire_selection.r_f_selected) {
      isHave = true;
      if (!xrSdk.cookie.get('r_f_tire')) {
        return false;
      }
    }
    if (tire_selection.l_e_selected) {
      isHave = true;
      if (!xrSdk.cookie.get('l_e_tire')) {
        return false;
      }
    }
    if (tire_selection.r_e_selected) {
      isHave = true;
      if (!xrSdk.cookie.get('r_e_tire')) {
        return false;
      }
    }
    return isHave;
  },
  nextClick:function(e){
    var that = this;
    // 檢查是否有正確輸入條形碼
    if (!that.checkTiresCode()) {
      util.toast("所选车轮方位全部录入完毕后才能点击下一步");
      return false;
    }
    app.WxService.navigateTo("../submit/submit", {});
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.showSuccess){
      wx.redirectTo({
        url: '../success/success'
      })
    }else{
      that.initTyre();
    }
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})